package com.ruoyi.book.service.impl;

import java.util.List;

import com.ruoyi.book.domain.Book;
import com.ruoyi.book.mapper.BookMapper;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.book.mapper.BorrowMapper;
import com.ruoyi.book.domain.Borrow;
import com.ruoyi.book.service.IBorrowService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 借书管理Service业务层处理
 *
 * @author ruoyi
 * @date 2022-12-30
 */
@Service
public class BorrowServiceImpl implements IBorrowService
{
    @Autowired
    private BorrowMapper borrowMapper;

    @Autowired
    private BookMapper bookMapper;

    /**
     * 查询借书管理
     *
     * @param id 借书管理主键
     * @return 借书管理
     */
    @Override
    public Borrow selectBorrowById(Long id)
    {
        return borrowMapper.selectBorrowById(id);
    }

    /**
     * 查询借书管理列表
     *
     * @param borrow 借书管理
     * @return 借书管理
     */
    @Override
    public List<Borrow> selectBorrowList(Borrow borrow)
    {
        return borrowMapper.selectBorrowList(borrow);
    }

    /**
     * 新增借书管理
     *
     * @param book 借书管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertBorrow(Book book)
    {
        Long bookId = book.getId();

        //用户是否借满三本书
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserId();
        Borrow borrow1 = new Borrow();
        borrow1.setUserId(userId);
        List<Borrow> borrowList=borrowMapper.getListByUserId(borrow1);
        if(borrowList.size()>3){
            throw new ServiceException("你已借书超过三本，无法继续借书");
        }

        //新增借书记录
        Borrow borrow = new Borrow();
        borrow.setBookId(bookId);
        borrow.setBookCover(book.getCover());
        borrow.setBookName(book.getName());
        borrow.setUserId(userId);
        borrow.setUserName(loginUser.getUser().getNickName());
        borrow.setReturnDate(book.getReturnDate());
        borrow.setCreateTime(DateUtils.getNowDate());
        int res = borrowMapper.insertBorrow(borrow);
        //更新书本信息
        book.setCount(book.getCount()-1);
        bookMapper.updateBook(book);
        return res;
    }

    /**
     * 修改借书管理
     *
     * @param borrow 借书管理
     * @return 结果
     */
    @Override
    public int updateBorrow(Borrow borrow)
    {
        borrow.setUpdateTime(DateUtils.getNowDate());
        return borrowMapper.updateBorrow(borrow);
    }

    /**
     * 批量删除借书管理
     *
     * @param ids 需要删除的借书管理主键
     * @return 结果
     */
    @Override
    public int deleteBorrowByIds(Long[] ids)
    {
        return borrowMapper.deleteBorrowByIds(ids);
    }

    /**
     * 删除借书管理信息
     *
     * @param id 借书管理主键
     * @return 结果
     */
    @Override
    public int deleteBorrowById(Long id)
    {
        return borrowMapper.deleteBorrowById(id);
    }

    @Override
    public List<Borrow> getListByUserId(Borrow borrow) {
        Long userId = SecurityUtils.getUserId();
        borrow.setUserId(userId);
        List<Borrow> res = borrowMapper.getListByUserId(borrow);
        return res;
    }
}
